define([
	'./libs/vuedraggble/sortable.js',
	'./libs/vuedraggble/vuedraggable.min.js'], function (require, exports) {
		/**
		 * 页面加载完成时触发
		 * @param hashCode 路由ID
		 * @param data 传递数据对象
		 * @param cite 页面站点信息
		 */
		exports.ready = function (hashCode, data, cite) {
			yufp.lookup.reg('CRUD_TYPE,NATIONALITY,PUBLISH_STATUS');
			yufp.custom.vue({
				el: cite.el,
				data: function () {
					var _this = this;
					/*定义校验规则，只能填字母数字 下划线*/
					var valiCodes = function (rule, value, callback) {
						var reg = /^[0-9a-zA-Z_]{1,}$/;
						if (value && reg.test(value)) {
							if (value.length > 64) {
								callback(new Error('长度不能大于64位'));
							} else {
								callback();
							}
						} else if (value && !reg.test(value)) {
							callback(new Error('能填字母、数字和下划线'));
						} else {
							callback();
						}
					};
					var hasLogOption = [{ key: 'N', value: '无' },
					{ key: 'Y', value: '有' }];
					var alarmModeOption = [{ key: 'S', value: '手机短信' }, { key: 'M', value: '电子邮件' }, { key: 'N', value: '控制台通知' }];
					var alarmClassOption = [{ key: 'E', value: '资源使用量危急' }, { key: 'W', value: '资源使用量预警' },
					{ key: 'L', value: '作业执行时间限制' }, { key: 'R', value: '作业返回值异常' },
					{ key: 'D', value: '作业启动延迟' }, { key: 'B', value: '作业开始前通知' },
					{ key: 'A', value: '作业结束后通知' }, { key: 'T', value: '作业执行时间异常' }]

					return {
						
						coderules: {
							templateCode: [
								{ validator: valiCodes, trigger: 'blur' }
							]
						},
						tbHeight:'0',
						disabledway: false,//编辑的时候警示方式不可修改
						addFnButton: !yufp.session.checkCtrl('addFn'),
						updateFnButton: !yufp.session.checkCtrl('updateFn'),
						deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
						treeDataUrl: "api/use/plats/paramValue/queryWarningTreeList",
						dataUrl: 'api/use/warning/template/Template',
						paramTableDataUrl: 'api/use/plats/paramValue/querySysVarList',
						realParamTableDataUrl: '',
						height: yufp.frame.size().height - 117,
						param: { UNITID: '0000', LEVELUNIT: '1' },
						formdata: {},
						searchFormdata: {},
						currentClickNode: {},
						selectDeleteData: [],
						addBtnShow: false,
						deleteBtnShow: true,
						changeBtnShow: true,
						alarmModeOption: alarmModeOption,
						alarmClassOption: alarmClassOption,
						hasLogOption: hasLogOption,
						isAddEvent: false,
						selectCount: 0,
						dialogVisible: false,
						hintDialogVisible: false,
						cancelBtnShow: true,
						async: false,
						viewTitleArrry: ['模板添加', '模板修改', '模版查看'],
						viewTitle: '',
						isView: false,
						isAddFlag: false
					};
				},
				mounted: function () {
					var _this = this;
					//测试
					_this.$nextTick(function(){
							var tableBox = _this.$refs.tableBox;
							let a = tableBox.getBoundingClientRect().top + 28;
							var theight = `calc(100vh - ${a}px - 64px)`;
							_this.tbHeight = theight;		
					});
				},
				methods: {

					reloadData: function () {
						var _this = this;
						var paramObj = _this.searchFormdata;
						paramObj.alarmClass = _this.currentClickNode.alarmClass;
						var param = { condition: JSON.stringify(paramObj) };
						_this.$refs.refTable.remoteData(param);
					},


					viewDataFn: function (data) {
						var _this = this;
						_this.dialogVisible = true;
						_this.viewTitle = _this.viewTitleArrry[2];
						_this.isReadOnly = false;
						_this.isView = true;
						_this.isAddFlag = false;
						var val = data;
						_this.realParamTableDataUrl = _this.paramTableDataUrl + "?valueLike=" + val.alarmClass;
						_this.$nextTick(function () {
							_this.$refs.refForm.resetFields();
							_this.formdata.alarmClass = val.alarmClass;
							_this.formdata.templateCode = val.templateCode;
							_this.formdata.templateName = val.templateName;
							_this.formdata.templateTitle = val.templateTitle;
							_this.formdata.alarmMode = val.alarmMode;
							_this.formdata.templateText = val.templateText;
							_this.formdata.logFlag = val.logFlag;
							_this.formdata.remark = val.remark;
						});
						_this.dragEvent();
					},

					dragEvent: function () {
						var _this = this;
						setTimeout(function () {
							var tree = document.querySelectorAll('.paramRefTableClass table>tbody')[0];
							console.log(tree)
							Sortable.create(tree, {
								group: { name: 'paramCodeDrag', pull: 'clone' }, sort: false
							});
							var title = document.querySelectorAll('.refFormClass div div div div input')[3];
							console.log(title)
							var text = document.querySelectorAll('.refFormClass div div div div div div div textarea')[0];
							console.log(text)
							Sortable.create(title, {
								group: { name: 'paramCodeDrag' },
								sort: false,
								onAdd: function (evt) {
									var tr = document.querySelectorAll('.refForm div tr')[0];
									var temp = {};
									var originData = _this.$refs.refForm.formdata.templateTitle;
									console.log(originData)
									if (originData !== undefined) {
										temp.templateTitle = originData + "{%" + _this.$refs.paramRefTable.tabledata[evt.oldIndex].paramCode + "%}";
									} else {
										temp.templateTitle = "{%" + _this.$refs.paramRefTable.tabledata[evt.oldIndex].paramCode + "%}";
									}
									yufp.extend(_this.formdata, temp);
								}
							});
							Sortable.create(text, {
								group: { name: 'paramCodeDrag' },
								sort: false,
								onAdd: function (evt) {
									var tr = document.querySelectorAll('.refForm div tr')[0];
									var temp = {};
									var originData = _this.$refs.refForm.formdata.templateText;
									console.log(originData)
									if (originData !== undefined) {
										temp.templateText = originData + "{%" + _this.$refs.paramRefTable.tabledata[evt.oldIndex].paramCode + "%}";
									} else {
										temp.templateText = "{%" + _this.$refs.paramRefTable.tabledata[evt.oldIndex].paramCode + "%}";
									}

									yufp.extend(_this.formdata, temp);
								}
							});
						}, 500);
					},
					changeEvent: function () {
						var _this = this;
						_this.disabledway = true;
						_this.isAddEvent = false;
						if (_this.$refs.refTable.selections.length !== 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
							return;
						}
						_this.dialogVisible = true;
						_this.viewTitle = _this.viewTitleArrry[1];
						_this.isReadOnly = false;
						_this.isView = false;
						_this.isAddFlag = false;
						var val = _this.$refs.refTable.selections[0];
						_this.realParamTableDataUrl = _this.paramTableDataUrl + "?valueLike=" + val.alarmClass;
						_this.$nextTick(function () {
							_this.$refs.refForm.resetFields();
							_this.formdata.alarmClass = val.alarmClass;
							_this.formdata.templateCode = val.templateCode;
							_this.formdata.templateName = val.templateName;
							_this.formdata.templateTitle = val.templateTitle;
							_this.formdata.alarmMode = val.alarmMode;
							_this.formdata.templateText = val.templateText;
							_this.formdata.logFlag = val.logFlag;
							_this.formdata.remark = val.remark;
						});
						_this.dragEvent();

					},
					deleteEvent: function () {

						var _this = this;
						if (_this.$refs.refTable.selections.length != 1) {
							_this.$message({ message: '请先至少选择一条记录', type: 'warning', duration: 6000 });
							return;
						}
						// 标准模板校验
						if (_this.$refs.refTable.selections[0].templateCode.indexOf("STD_") != -1) {
							_this.$message({ message: '不能删除标准模板', type: 'warning', duration: 6000 });
							return;
						}

						_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning', duration: 6000,
							center: true,
							callback: function (action) {
								if (action === 'confirm') {
									yufp.service.request({
										method: 'POST',
										url: "api/use/warning/template/batchDelete",
										data: JSON.stringify(_this.$refs.refTable.selections),
										callback: function (code, message, response) {
											if (response.code == 0) {
												_this.reloadData();
												_this.$message('操作成功');
											} else {
												successFlag = false;
												_this.$message({ message: response.message, type: "warning" });
											}
										}
									});
								}
							}
						});
					},
					/*  deleteFn: function(){
								var _this = this;
								var data = _this.selectDeleteData;
								let urlparam = 'api/use/warning/template/delete';

								yufp.service.request({
										method: 'POST',
										url:"api/use/warning/template/batchDelete",
										data: JSON.stringify(data),
										callback: function (code, message, response) {
												if(response.code === 0) {
														_this.$refs.refTable.remoteData("");
														_this.$message('操作成功');
												}else{
														successFlag = false;
														_this.$message(response.message);
												}
										}
								});
								this.hintDialogVisible = false;
						},*/
					selectEvent: function (selection) {
						var _this = this;
						_this.selectCount = _this.$refs.refTable.selections.length;
					},
					stateFormatter: function (row, column, cellValue) {
						if (cellValue !== null) {
							return "已记录"
						} else {
							return "未记录"
						}
					},
					addEvent: function () {
						var _this = this;
						_this.dialogVisible = true;
						_this.viewTitle = _this.viewTitleArrry[0];
						_this.disabledway = false;
						_this.isReadOnly = false;
						_this.isAddEvent = true;
						_this.isAddFlag = true;
						_this.isView = false;
						_this.realParamTableDataUrl = _this.paramTableDataUrl + "?valueLike=" + _this.currentClickNode.paramCode;
						_this.$nextTick(function () {
							_this.$refs.refForm.resetFields();
						});
						_this.dragEvent();
					},
					saveFn: function () {
						var _this = this;
						var validate = false;
						_this.$refs.refForm.validate(function (valid) {
							validate = valid;
						});
						if (!validate) {
							return;
						}
						let urlparam = "";
						var paramObj = _this.$refs.refForm.formdata;
						// 模板代码校验
						if (paramObj.templateCode && paramObj.templateCode.indexOf("STD_") != -1) {
							if (_this.isAddEvent === true) {
								_this.$message({ message: '请不要使用"STD_"作为模板编码内容', type: 'warning', duration: 6000 });
								return;
							} else {
								_this.$message({ message: '标准模板不允许修改', type: 'warning', duration: 6000 });
								return;
							}
						}
						if (_this.isAddEvent === true) {
							urlparam = 'api/use/warning/template/';
							paramObj.alarmClass = _this.currentClickNode.paramCode

						} else {
							urlparam = 'api/use/warning/template/update';
						}
						yufp.service.request({
							method: 'POST',
							url: urlparam,
							data: {
								alarmClass: paramObj.alarmClass,
								alarmMode: paramObj.alarmMode,
								logFlag: paramObj.logFlag,
								remark: paramObj.remark,
								templateCode: paramObj.templateCode,
								templateName: paramObj.templateName,
								templateText: paramObj.templateText,
								templateTitle: paramObj.templateTitle
							},
							callback: function (code, message, response) {

								if (response.code == 0) {


									_this.$message('操作成功');
									_this.reloadData();
									_this.dialogVisible = false;

								} else {
									_this.$message(response.message);
								}

							}
						});

					},
					cancelFn: function () {
						var _this = this;
						this.dialogVisible = false;
						this.hintDialogVisible = false;
					},
					/**
					 * 点击树的结点，加载对应的数据
					 */
					nodeClickFn: function (nodeData, node, self) {
						var _this = this;
						_this.currentClickNode = nodeData;
						var alarmClass = "";
						if (nodeData.paramCode === '0000') {
							_this.addBtnShow = false;
							alarmClass = "";
						} else {
							_this.addBtnShow = true;
							alarmClass = nodeData.paramCode;
						}
						var paramObj = {};
						if (alarmClass !== "") {
							paramObj.alarmClass = alarmClass;
						}
						_this.currentClickNode.alarmClass = alarmClass;
						_this.reloadData();
					}
				},
			});
		};
	});